package com.ysd.lis.service.eq.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.eq.*;
import com.ysd.lis.mapper.eq.*;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.eq.EqEquiptHomeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-29
 */
@Service
public class EqEquiptHomeServiceImpl implements EqEquiptHomeService {

    @Autowired
    EqRepairOrderMapper eqRepairOrderMapper;
    @Autowired
    EqMaintainOrderMapper eqMaintainOrderMapper;
    @Autowired
    EqMaintainPlanMapper eqMaintainPlanMapper;
    @Autowired
    EqRepairPlanMapper eqRepairPlanMapper;
    @Autowired
    EqEquipmentMapper eqEquipmentMapper;
    @Autowired
    EqPatrolTaskMapper eqPatrolTaskMapper;
    @Autowired
    EqPatrolTaskEqMapper eqPatrolTaskEqMapper;
    @Override
    public Result getPersonalAgency(CommonPageDto pageParam) {
        Map<String,Object> returnMap = new HashMap<>();
        //维修计划
        String[] rPlanStatuss = new String[]{"5", "10"};
        LambdaQueryWrapper<EqRepairPlan> queryWrapperMR = new LambdaQueryWrapper<>();
        queryWrapperMR.eq(EqRepairPlan::getDelFlag,0)
                .in(EqRepairPlan::getPlanStatus,rPlanStatuss);
        Integer rPlanCnt = eqRepairPlanMapper.selectCount(queryWrapperMR);
        returnMap.put("rPlanCnt",rPlanCnt);

        //保养计划
        String[] mPlanStatuss = new String[]{"5", "10"};
        LambdaQueryWrapper<EqMaintainPlan> queryWrapperMP = new LambdaQueryWrapper<>();
        queryWrapperMP.eq(EqMaintainPlan::getDelFlag,0)
                .in(EqMaintainPlan::getPlanStatus,mPlanStatuss);
        Integer mPlanCnt = eqMaintainPlanMapper.selectCount(queryWrapperMP);
        returnMap.put("mPlanCnt",mPlanCnt);

        //保养任务
        String[] maintainStatuss = new String[]{"0", "5"};
        LambdaQueryWrapper<EqMaintainOrder> queryWrapperM = new LambdaQueryWrapper<>();
        queryWrapperM.eq(EqMaintainOrder::getDelFlag,0)
                .in(EqMaintainOrder::getMaintainStatus,maintainStatuss);
        Integer maintainCnt = eqMaintainOrderMapper.selectCount(queryWrapperM);
        returnMap.put("maintainCnt",maintainCnt);

        //维修任务
        String[] repairStatuss = new String[]{"0", "5","10", "15"};
        LambdaQueryWrapper<EqRepairOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqRepairOrder::getDelFlag,0)
                .in(EqRepairOrder::getRepairStatus,repairStatuss);
        Integer repairCnt = eqRepairOrderMapper.selectCount(queryWrapper);
        returnMap.put("repairCnt",repairCnt);
        //设备总数
        LambdaQueryWrapper<EqEquipment> queryWrapperEq = new LambdaQueryWrapper<>();
        queryWrapperEq.eq(EqEquipment::getDelFlag,0);
        Integer equiptCnt = eqEquipmentMapper.selectCount(queryWrapperEq);
        returnMap.put("equiptCnt",equiptCnt);

        //故障台数
        String[] equiptRepairStatuss = new String[]{"0", "5","10", "15","20"};
        MPJLambdaWrapper<EqRepairOrder> queryWrapperEr = new MPJLambdaWrapper<>();
        queryWrapperEr.select("DISTINCT(EQUIPT_ID)")
                .innerJoin(EqEquipment.class,"eq", p->p.eq(EqEquipment::getId,EqRepairOrder::getEquiptId).eq(EqEquipment::getDelFlag,0))
                .in(EqRepairOrder::getRepairStatus,equiptRepairStatuss)
                .eq(EqRepairOrder::getDelFlag,0);
        Integer equiptRepairCnt = eqRepairOrderMapper.selectCount(queryWrapperEr);
        returnMap.put("equiptRepairCnt",equiptRepairCnt);



        return Result.succ(1, "查询成功", returnMap);

    }

    @Override
    public Result getPersonalAgency2(CommonPageDto pageParam) {
        Map<String,Object> returnMap = new HashMap<>();
        //设备使用状态
        MPJLambdaWrapper<EqEquipment> queryWrapperEqU = new MPJLambdaWrapper<>();
        queryWrapperEqU.selectCount(EqEquipment::getUsingStatus, EqEquipment::getUsingStatusCnt)
                .selectAs(EqEquipment::getUsingStatus, EqEquipment::getUsingStatus)
                .eq(EqEquipment::getDelFlag,0)
                .groupBy(EqEquipment::getUsingStatus);
        List<EqEquipment> equiptUsingCntList = eqEquipmentMapper.selectList(queryWrapperEqU);
        returnMap.put("equiptUsingCntList",equiptUsingCntList);
        //设备维修
        MPJLambdaWrapper<EqRepairOrder> queryWrapperEqO = new MPJLambdaWrapper<>();
        queryWrapperEqO.selectCount(EqRepairOrder::getRepairStatus, EqRepairOrder::getRepairStatusCnt)
                .selectAs(EqRepairOrder::getRepairStatus, EqRepairOrder::getRepairStatus)
                .eq(EqRepairOrder::getDelFlag,0)
                .groupBy(EqRepairOrder::getRepairStatus);
        List<EqRepairOrder> repairStatusCntList = eqRepairOrderMapper.selectList(queryWrapperEqO);
        returnMap.put("repairStatusCntList",repairStatusCntList);

        //设备维修 今日新增
        Calendar calendar = Calendar.getInstance(); // 获取当前日期的Calendar实例
        Date today = calendar.getTime();
        LambdaQueryWrapper<EqRepairOrder> queryWrapperRCNT = new LambdaQueryWrapper<>();
        queryWrapperRCNT.eq(EqRepairOrder::getDelFlag,0)
                .ge(EqRepairOrder::getCreateTime, DateUtil.beginOfDay(today).toTimestamp())
                .le(EqRepairOrder::getCreateTime, DateUtil.endOfDay(today).toTimestamp());
        Integer repairJRXZCnt = eqRepairOrderMapper.selectCount(queryWrapperRCNT);
        returnMap.put("repairJRXZCnt",repairJRXZCnt);

        //设备保养

        MPJLambdaWrapper<EqMaintainOrder> queryWrapperMaO = new MPJLambdaWrapper<>();
        queryWrapperMaO.selectCount(EqMaintainOrder::getMaintainStatus, EqMaintainOrder::getMaintainStatusCnt)
                .selectAs(EqMaintainOrder::getMaintainStatus, EqMaintainOrder::getMaintainStatus)
                .eq(EqMaintainOrder::getDelFlag,0)
                .groupBy(EqMaintainOrder::getMaintainStatus);
        List<EqMaintainOrder> maintainStatusCntList = eqMaintainOrderMapper.selectList(queryWrapperMaO);
        returnMap.put("maintainStatusCntList",maintainStatusCntList);

        //超期未保养
        Timestamp timestamp = new Timestamp(today.getTime());

        LambdaQueryWrapper<EqMaintainOrder> queryWrapperMaO1 = new LambdaQueryWrapper<>();
        queryWrapperMaO1.eq(EqMaintainOrder::getDelFlag,0)
                .ne(EqMaintainOrder::getMaintainStatus,"15")
                .isNotNull(EqMaintainOrder::getPlanDate)
                .le(EqMaintainOrder::getPlanDate, timestamp);
        Integer maintainOverdueCnt = eqMaintainOrderMapper.selectCount(queryWrapperMaO1);
        returnMap.put("maintainOverdueCnt",maintainOverdueCnt);

        //巡检任务概况
        MPJLambdaWrapper<EqPatrolTask> patrolTaskWrapper = new MPJLambdaWrapper<>();
        patrolTaskWrapper.selectCount(EqPatrolTask::getStatus, EqRepairOrder::getRepairStatusCnt)
                .selectAs(EqPatrolTask::getStatus, EqPatrolTask::getStatus)
                .eq(EqPatrolTask::getDelFlag,0)
                .groupBy(EqPatrolTask::getStatus);
        List<EqPatrolTask> patrolTaskList = eqPatrolTaskMapper.selectList(patrolTaskWrapper);
        returnMap.put("patrolTaskCnList",patrolTaskList);

        //巡检设备概况
        MPJLambdaWrapper<EqPatrolTaskEq> patrolTaskEqWrapper = new MPJLambdaWrapper<>();
        patrolTaskEqWrapper.selectAll(EqPatrolTaskEq.class)
                .eq(EqPatrolTaskEq::getDelFlag,0) ;
        List<EqPatrolTaskEq> patrolTaskEqList = eqPatrolTaskEqMapper.selectList(patrolTaskEqWrapper);
        long noPassCount = patrolTaskEqList.stream().filter(item -> StringUtils.isNotBlank(item.getNoPass())).filter(item -> StringUtils.isBlank(item.getNoCheck())).map(BaseEntity::getId).count();
        long noDetectionCount = patrolTaskEqList.stream().filter(item -> StringUtils.isNotBlank(item.getNoCheck())).map(BaseEntity::getId).count();
        long rigntCount = patrolTaskEqList.stream().filter(item -> StringUtils.isBlank(item.getNoPass())).filter(item -> StringUtils.isBlank(item.getNoCheck())).map(BaseEntity::getId).count();
        Map<String,Object> map=new HashMap<>();
        map.put("noPassCount",noPassCount);
        map.put("noDetecttionCount",noDetectionCount);
        map.put("rigntCount",rigntCount);
        map.put("shouldInspected",patrolTaskEqList.size());
        map.put("realityInspected",noDetectionCount+rigntCount);
        returnMap.put("eqPatrolTaskEqInfo",map);
        return Result.succ(1, "查询成功", returnMap);
    }
}
